package com.woniuxy.cloud.carl.car.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.cloud.Aspect.log.Log;
import com.woniuxy.cloud.carl.car.client.CarManageClient;
import com.woniuxy.cloud.carl.car.client.param.AddBrandParam;
import com.woniuxy.cloud.carl.car.client.param.AddCarParam;
import com.woniuxy.cloud.carl.car.client.param.AddSortParam;
import com.woniuxy.cloud.carl.car.client.param.AddStyleParam;
import com.woniuxy.cloud.carl.car.common.CarResultCode;
import com.woniuxy.cloud.carl.car.infra.model.*;
import com.woniuxy.cloud.carl.car.infra.service.*;
import com.woniuxy.exceptions.MallException;
import com.woniuxy.result.Result;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class CarManageApiImpl implements CarManageClient {
    @Resource
    private CarBrandService brandService;
    @Resource
    private CarSortService sortService;
    @Resource
    private CarRepositoryService styleService;
    @Resource
    private ProductYearService yearService;
    @Resource
    private CarService carService;

    // 品牌 --车系 -- 排量 -- 车型 -- 生产年份 --car
    @Override
    @Log(value = "新增汽车")
    public Result insertCar(AddCarParam param) {
        //判断参数
        CarBrand brand = brandService.getById(param.getCarBrandId());
        if(brand==null) throw new MallException(CarResultCode.BRAND_NOT_EXIST);
        CarSort carSort = sortService.getById(param.getCarSortId());
        if(carSort==null) throw new MallException(CarResultCode.SORT_NOT_EXIST);
        CarRepository carStyle = styleService.getById(param.getCarStyleId());
        if(carStyle==null) throw new MallException(CarResultCode.CAR_STYLE_NOT_EXIST);
        ProductYear year = yearService.getById(param.getProductYearId());
        if(year==null) throw new MallException(CarResultCode.YEAR_NOT_EXIST);

        //判断是否已存入
        QueryWrapper<Car> wrapper = new QueryWrapper<Car>().eq("product_year_id", param.getProductYearId()).eq("car_style_id", param.getCarStyleId());
        Car car = carService.getOne(wrapper);
        if(car!=null) throw new MallException(CarResultCode.CAR_IS_EXIST);
        //拼接新车的数据
        Car newCar = BeanUtil.toBean(param, Car.class);
        String desc = brand.getCarBrandName()+" "+carSort.getCarSortName()+" "+carStyle.getDisplacement()+" "+carStyle.getStyleName()+" "+year.getYearName();
        newCar.setCarDesc(desc);
        carService.save(newCar);
        return Result.success(newCar);
    }

    @Override
    @Log(value = "新增品牌")
    public Result insertBrand(AddBrandParam param) {
        CarBrand carBrand = brandService.getOne(new QueryWrapper<CarBrand>().eq("car_brand_name", param.getCarBrandName()));
        if(carBrand!=null) throw new MallException(CarResultCode.BRAND_IS_EXIST);
        CarBrand b = BeanUtil.toBean(param, CarBrand.class);
        brandService.save(b);
        return Result.success(b);
    }

    @Override
    @Log(value = "新增车型")
    public Result insertStyle(AddStyleParam param) {
        CarSort sort = sortService.getById(param.getSortId());
        if(sort==null) throw new MallException(CarResultCode.SORT_NOT_EXIST);
        CarRepository one = styleService.getOne(new QueryWrapper<CarRepository>().eq("sort_id", param.getSortId()).eq("style_name", param.getStyleName()).eq("displacement", param.getDisplacement()));
        if(one!=null) throw new MallException(CarResultCode.STYLE_IS_EXIST);
        CarRepository carStyle = BeanUtil.toBean(param, CarRepository.class);
        styleService.save(carStyle);
        return Result.success(carStyle);
    }

    @Override
    @Log(value = "新增车系")
    public Result insertSort(AddSortParam param) {
        CarBrand brand = brandService.getById(param.getCarBrandId());
        if(brand==null) throw new MallException(CarResultCode.BRAND_NOT_EXIST);
        CarSort carSort = sortService.getOne(new QueryWrapper<CarSort>().eq("car_sort_name", param.getCarSortName()));
        if(carSort!=null) throw new MallException(CarResultCode.SORT_IS_EXIST);
        CarSort sort = BeanUtil.toBean(param, CarSort.class);
        sortService.save(sort);
        return Result.success(sort);
    }

    @Override
    @Log(value = "新增年份")
    public Result insertYear(String yearName) {
        ProductYear year = yearService.getOne(new QueryWrapper<ProductYear>().eq("year_name", yearName));
        if(year!=null) throw new MallException(CarResultCode.YEAR_IS_EXIST);
        ProductYear productYear = new ProductYear();
        productYear.setYearName(yearName);
        yearService.save(productYear);
        return Result.success(productYear);
    }


}
